package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Activities;
import company.domain.Rescuestations;
import company.domain.Users;
import company.service.ActivitiesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ActivitiesController {
    @Resource
    ActivitiesService activitiesService;

    /*
    * 分页查询
    * */
    @RequestMapping("/selectAllActivties/{page}")
    public R selectAllActivties(@PathVariable("page") int page,@RequestBody Activities activities){
        QueryWrapper<Activities> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(activities.getName()!="" && !("".equals(activities.getName())),"name",activities.getName());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,2);
        List<Activities> list=this.activitiesService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
     * 新增公告
     * */
    @PostMapping("/AddActivties")
    public R AddActivties(@RequestBody Activities activities){
        this.activitiesService.save(activities);
        return new R();
    }

    /*
     * 公告删除
     * */
    @RequestMapping("/Delactivities/{id}")
    public R Delactivities(@PathVariable("id") int id){
        boolean remove = this.activitiesService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据公告id查询
     * */
    @PostMapping("/selectActivitiesbyid")
    public R selectActivitiesbyid(@RequestBody Activities activities){
        Activities byId = this.activitiesService.getById(activities.getActivityid());
        return new R().add("byId",byId);
    }

    /*
     * 修改公告
     * */
    @PostMapping("/uploadActivities")
    public R uploadActivities(@RequestBody Activities activities){
        this.activitiesService.updateById(activities);
        return new R();
    }
}
